/**
 * 
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import entity.Admin;
import entity.Message;
import utils.DBHelper;

/**
 * @author luoxue
 *
 */
public class LeaveMessageDAO {

	private Connection c;
	private Statement s;
	private PreparedStatement ps;
	private ResultSet rs;
	public LeaveMessageDAO() {
		super();
		// TODO Auto-generated constructor stub
	}
	public static LeaveMessageDAO getInstance() {
		return new LeaveMessageDAO();
	}

	public void SendMessage(int send_id,int accept_id,String context)
	{
		
		try {
			c = DBHelper.getInstance().getConnection();

			String sql = "insert into message values(?,?,?,null)";
			ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			ps.setInt(1, send_id);
			ps.setInt(2, accept_id);
			ps.setString(3, context);
			ps.execute();
			//如果可以搜索到
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeConnection(c, ps, rs);
		}
	}
	public ArrayList<Message> getMymessages()
	{
	
		ArrayList<Message> s = new ArrayList<Message>();
		
		try{
		c = DBHelper.getInstance().getConnection();
		String sql = "select * from message";
		ps = c.prepareStatement(sql);
		rs = ps.executeQuery();
		while(rs.next())
		{
			Message one = new Message();
			one.setAccept_id(rs.getInt("accept_id"));
			one.setContext(rs.getString("context"));
			one.setSend_id(rs.getInt("send_id"));
			one.setID(rs.getInt("ID"));
			s.add(one);
		}
		
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		finally{
			DBHelper.closeConnection(c, ps, rs);
		}
		
		return s;	
	}
	public void DeleteMymessage(int ID)
	{
	
		
		
		try{
		c = DBHelper.getInstance().getConnection();
		String sql = "delete from message where ID = ?";
		ps = c.prepareStatement(sql);
		ps.setInt(1, ID);
		ps.execute();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		finally{
			DBHelper.closeConnection(c, ps, rs);
		}
			
	}
	public String getName(int ID)
	{
	
		String s =new String();
		
		try{
		c = DBHelper.getInstance().getConnection();
		String sql = "select * from user where user_id = ?";
		ps = c.prepareStatement(sql);
		ps.setInt(1, ID);
		rs = ps.executeQuery();
		while(rs.next())
		{
			s = rs.getString("user_name");
		}
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		finally{
			DBHelper.closeConnection(c, ps, rs);
		}
		return s;
			
	}
}
